﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Sharp.Portal.Configuration;

namespace Sharp.Portal.ExtNet.FormField
{
    /// <summary>
    /// 表单元素基类
    /// </summary>
    public class ElementField
    {
        /// <summary>
        /// 表单元素基类
        /// </summary>
        public ElementField()
        {
            JsBuilder = new List<string>();
            IsDebug = true;
            AutoId = true;
        }


        /// <summary>
        /// 标题
        /// </summary>
        public string FieldLabel { get; set; }
 

        #region 响应客户端更新
        /// <summary>
        /// 响应客户端更新
        /// </summary>
        public bool ClientUpdate
        {
            get;
            set;
        }
        #endregion

        /// <summary>
        /// 打开方式
        /// </summary>
        public OpenWith LayoutType { get; set; }

        /// <summary>
        /// 调试状态
        /// </summary>
        public bool IsDebug
        {
            get;
            set;
        }


        /// <summary>
        /// 用于查询的默认值
        /// </summary>
        public object QueryDefaultValue
        {
            get;
            set;
        }


        /// <summary>
        /// 浏览模式
        /// </summary>
        public virtual bool IsBrowsing()
        {
            return Convert.ToBoolean(HttpContext.Current.Request.Params["IsReadOnlyForm"]);
        }

        /// <summary>
        /// 浏览模式下保持 XType 属性不变
        /// </summary>
        protected bool KeepingXType { get; set; }



        /// <summary>
        /// Js 构建内存列表
        /// </summary>
        protected List<string> JsBuilder { get; set; }

        /// <summary>
        /// 字段属性列表
        /// </summary>
        internal Dictionary<string, string> FieldProperties = new Dictionary<string, string>();

        /// <summary>
        /// 加入表单字段属性
        /// </summary>
        /// <param name="propertyName">属性名字</param>
        /// <param name="value">布尔值属性值</param>
        public void AddFieldProperty(string propertyName, Boolean value)
        {
            AddFieldProperty(propertyName, value ? "true" : "false", true);
        }

        /// <summary>
        /// 加入表单字段属性
        /// </summary>
        /// <param name="propertyName">属性名字</param>
        /// <param name="value">整数属性值</param>
        public void AddFieldProperty(string propertyName, int? value)
        {
            AddFieldProperty(propertyName, Convert.ToString(value), true);
        }

        /// <summary>
        /// 加入表单字段属性
        /// </summary>
        /// <param name="propertyName">属性名字</param>
        /// <param name="value">浮点属性值</param>
        public void AddFieldProperty(string propertyName, double? value)
        {
            AddFieldProperty(propertyName, Convert.ToString(value), true);
        }

        /// <summary>
        /// 加入表单字段属性
        /// </summary>
        /// <param name="propertyName">属性名字</param>
        /// <param name="value">日期属性值</param>
        public void AddFieldProperty(string propertyName, DateTime value)
        {
            FieldProperties.Add(propertyName, value.ToString());
        }

        /// <summary>
        /// 加入表单字段属性
        /// </summary>
        /// <param name="propertyName">属性名字</param>
        /// <param name="value">字符串属性值</param>
        /// <param name="entryString">实体字符串，不加''</param>
        public void AddFieldProperty(string propertyName, string value, bool entryString = false)
        {
            if (!FieldProperties.ContainsKey(propertyName))
                FieldProperties.Add(propertyName, string.Empty);
            if (entryString || propertyName == "regex")
                FieldProperties[propertyName] = value;
            else
            {
                FieldProperties[propertyName] = value.ToJson();
            }
        }

        /// <summary>
        /// 监听器
        /// </summary>
        private readonly Dictionary<string, string> _listeners = new Dictionary<string, string>();

        /// <summary>
        /// 增加事件
        /// </summary>
        /// <param name="eventName">事件名称</param>
        /// <param name="function">函数定义脚本</param>
        public void AddListener(string eventName, string function)
        {
            if (_listeners.ContainsKey(eventName))
                _listeners[eventName] = function;
            else
                _listeners.Add(eventName, function);
        }

        /// <summary>
        /// 检查表单字段属性是否存在
        /// </summary>
        /// <param name="propertyName">属性名字</param>
        public Boolean HasFieldProperty(string propertyName)
        {
            return FieldProperties.ContainsKey(propertyName);
        }

        /// <summary>
        /// 移除表单字段属性
        /// </summary>
        /// <param name="propertyName">属性名字</param>
        public void RemoveFieldProperty(string propertyName)
        {
            if (FieldProperties.ContainsKey(propertyName))
                FieldProperties.Remove(propertyName);
        }



        /// <summary>
        /// 操作类型
        /// </summary>
        protected string OperationMode
        {
            get
            {
                return HttpContext.Current.Request.QueryString["hs"];
            }
        }
        /// <summary>
        /// 字段名称
        /// </summary>
        public string FieldName { get; set; }
        /// <summary>
        /// 自动字段id
        /// </summary>
        internal bool AutoId { get; set; }

        /// <summary>
        /// 生成
        /// 需要在派生类中重载
        /// </summary>
        /// <param name="nonFirst">当前容器为空</param>
        /// <returns>JS 字符串</returns>
        public virtual string Build(bool nonFirst = false)
        {
           
            //var config = SystemConfigManage.Read<PortalConfig>();

            //AddFieldProperty("labelSeparator", config.LabelSeparator);
            //AddFieldProperty("labelAlign", config.LabelAlign);

            if (AutoId)
                AddFieldProperty("AutoId", null);


            // 事件属性

            //string events = string.Join(",", this._listeners.Select(x => string.Format("{0}:{1}", x.Key, x.Value)));

            //if (!events.IsNullOrEmpty())
            //{
            //    AddFieldProperty("listeners", "{" + events + "}", true);
            //}

            //AddFieldProperty("Hidden",string.Format( "this.CheckFieldPermission(\"{0}\")",this.FieldName), true);

            StringBuilder sbJs = new StringBuilder();
            if (FieldProperties.Count > 0)
            {
                sbJs.Append(".");
            }
            sbJs.Append(string.Join(".", FieldProperties.Select(x => string.Format("{0}({1})", x.Key, x.Value))));
            //sbJs.Append(string.Join("\r\n", JsBuilder.ToArray()));

             
            return sbJs.ToString();
        }
    }
}
